﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Text;

public partial class MasterPage_MasterPageIndex : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            CheckLogin();
            BindMessage();
        }
    }

    #region 登陆
    protected void ibSubmit_Click(object sender, ImageClickEventArgs e)
    {
        string username = functions.chgSql(txtUserName.Text.Trim());
        string password = functions.chgSql(txtPassword.Text.Trim());

        string sqlcheck = "select userid,username,realname,image,phone,levelid from users where username=@username and password=@password and flag=0";

        SqlParameter[] parms = new SqlParameter[2];
        parms[0] = new SqlParameter("@username", SqlDbType.NVarChar);
        parms[1] = new SqlParameter("@password", SqlDbType.NVarChar);
        parms[0].Value = username;
        parms[1].Value = password;

        DataSet ds = new DataSet();
        ds = DataClass.GetDataSet(sqlcheck, parms);

        if (ds != null && ds.Tables[0].Rows.Count > 0)
        {
            string userid = Convert.ToString(ds.Tables[0].Rows[0]["userid"] + "");
            string realname = Convert.ToString(ds.Tables[0].Rows[0]["realname"] + "");
            string image = Convert.ToString(ds.Tables[0].Rows[0]["image"] + "");
            string levelid = Convert.ToString(ds.Tables[0].Rows[0]["levelid"] + "");

            Session["userid"] = userid;
            Session["username"] = username;
            Session["levelid"] = levelid;
            Session["image"] = image;
            Session["realname"] = realname;

            //插入登录日志
            Users.InsertUserLoginLog(userid);
            string url = Request.Url.ToString();
            Response.Redirect(url);
        }
        else
        {
            lblMessage.Text = "<script>alert('用户名或密码错误！')</script>";
            return;
        }
    }
    #endregion

    #region 判断是否登陆
    private void CheckLogin()
    {
        if (Session["userid"] + "" == "")//没有登录
        {
            divlogin.Visible = true;
            divwelcome.Visible = false;
        }
        else
        {
            divlogin.Visible = false;
            divwelcome.Visible = true;

            //加载信息
            lblRealName.Text = Session["realname"] + "";
            string levelname = Level.GetLevelNameByLevelID(Session["levelid"] + "");
            string point = Convert.ToString(UserPoint.GetUserPointAvailable(Session["userid"] + ""));
            lblLevel.Text = levelname;
            lblPoint.Text = point;
        }
    }
    #endregion

    #region 加载公告栏
    private void BindMessage()
    {
        DataSet dsMessage = new DataSet();
        dsMessage = Message.GetMessage("5");
        dlMessage.DataSource = dsMessage;
        dlMessage.DataBind();
    }
    #endregion
}
